/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.globussoft.util;

/**
 *
 * @author Globussoft
 */
public class FormatedFileSize {
public static long getSelectedSizeFromFormatedString(String filesize) {//1 GB 2 MB 4 KB 820 bytes
        long size = 0;
        if (filesize.contains("GB")) {
            size = (long) Float.parseFloat(filesize.substring(0, filesize.indexOf("GB")).trim()) * 1024 * 1024 * 1024;
            filesize = filesize.substring(filesize.indexOf("GB") + "GB".length()).trim();
        }
        if (filesize.contains("MB")) {
            size = size + (long) Float.parseFloat(filesize.substring(0, filesize.indexOf("MB")).trim()) * 1024 * 1024;
            filesize = filesize.substring(filesize.indexOf("MB") + "MB".length()).trim();
        }
        if (filesize.contains("KB")) {
            size = size + (long)Float.parseFloat(filesize.substring(0, filesize.indexOf("KB")).trim()) * 1024;
            filesize = filesize.substring(filesize.indexOf("KB") + "KB".length()).trim();
        }
        if (filesize.contains("bytes")) {
            size = size + (long) Float.parseFloat(filesize.substring(0, filesize.indexOf("bytes")).trim());
            filesize = filesize.substring(filesize.indexOf("bytes") + "bytes".length()).trim();
        }

        return size;
    }
public static String getFormatedFileSize(long filesize) {
        String size = "";
        if (filesize >= 1024 * 1024 * 1024) {
            size = String.valueOf(filesize / (1024 * 1024 * 1024)) + " " + "GB ";
            filesize = filesize % (1024 * 1024 * 1024);
        }
        if (filesize >= 1024 * 1024) {
            size = size + String.valueOf(filesize / (1024 * 1024)) + " " + "MB ";
            filesize = filesize % (1024 * 1024);
        }
        if (filesize >= 1024) {
            size = size + String.valueOf(filesize / (1024)) + " " + "KB ";
            filesize = filesize % (1024);
        }
        if (filesize > 0) {
            size = size + String.valueOf(filesize) + " " + "bytes";
        }
        return size;
    }
}
